package com.nervenets.web.redis;

import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCommands;
import redis.clients.jedis.Tuple;

import javax.annotation.Resource;
import java.util.Set;

/**
 * Created by Joe on 2017/12/16.
 */
@Component
public class RedisOrderedSetDao {
    @Resource
    private
    JedisCommands jedisCluster;

    public <T extends RedisOrderedSetDomain> void saveOrUpdate(T domain) {
        jedisCluster.zadd(domain.getRedisKey(), domain.getOrderedValue(), domain.getField());
        if (domain.getExpire() > 0) {
            jedisCluster.expire(domain.getRedisKey(), domain.getExpire());
        }
    }

    public <T extends RedisOrderedSetDomain> void saveOrUpdate(T... domain) {
        for (T t : domain) {
            saveOrUpdate(t);
        }
    }

    public <T extends RedisOrderedSetDomain> Double getZScore(T domain) {
        return jedisCluster.zscore(domain.getRedisKey(), domain.getField());
    }

    public <T extends RedisOrderedSetDomain> Long getZCount(T domain, double min, double max) {
        return jedisCluster.zcount(domain.getRedisKey(), min, max);
    }

    public <T extends RedisOrderedSetDomain> Long getZCard(T domain) {
        return jedisCluster.zcard(domain.getRedisKey());
    }

    public <T extends RedisOrderedSetDomain> Long getZRank(T domain) {
        return jedisCluster.zrank(domain.getRedisKey(), domain.getField());
    }

    public <T extends RedisOrderedSetDomain> Long getZRevRank(T domain) {
        return jedisCluster.zrevrank(domain.getRedisKey(), domain.getField());
    }

    public <T extends RedisOrderedSetDomain> Set<String> getZListAsc(T domain, long startPage, long pageSize) {
        return jedisCluster.zrange(domain.getRedisKey(), (startPage - 1) * pageSize, (startPage - 1) * pageSize + (pageSize - 1));
    }

    public <T extends RedisOrderedSetDomain> Set<String> getZListDesc(T domain, long startPage, long pageSize) {
        return jedisCluster.zrevrange(domain.getRedisKey(), (startPage - 1) * pageSize, (startPage - 1) * pageSize + (pageSize - 1));
    }

    public <T extends RedisOrderedSetDomain> Set<Tuple> getZListWithScoresDesc(T domain, long startPage, long pageSize) {
        return jedisCluster.zrevrangeWithScores(domain.getRedisKey(), (startPage - 1) * pageSize, (startPage - 1) * pageSize + (pageSize - 1));
    }

    public <T extends RedisOrderedSetDomain> Set<String> getZListAsc(T domain, double min, double max, long startPage, long pageSize) {
        return null;
    }

    public <T extends RedisOrderedSetDomain> Set<String> getZListDesc(T domain, double min, double max, long startPage, long pageSize) {
        return null;
    }

    public <T extends RedisOrderedSetDomain> void delete(T domain) {
        jedisCluster.zrem(domain.getRedisKey(), domain.getField());
    }

    public Double incrBy(RedisOrderedSetDomain domain, long offset) {
        return jedisCluster.zincrby(domain.getRedisKey(), offset, domain.getField());
    }

    public void incr(RedisOrderedSetDomain domain) {
        jedisCluster.zincrby(domain.getRedisKey(), 1, domain.getField());
    }
}
